package cn.wp.common.core.plugin.scheduler;

import cn.hutool.core.date.DateUtil;
import cn.wp.common.core.Wp;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.web.annotation.ClearInterceptor;
import cn.wp.common.core.web.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 调度服务接口
 * Created by chenyb on 2019/10/23
 */
@RestController
@RequestMapping(Wp.WebCommonBase + "/scheduler")
@ClearInterceptor(LoginInterceptor.class)
public class SchedulerController {

    @Autowired
    private SchedulerService schedulerService;

    /**
     * 测试任务
     * @return
     */
    @GetMapping("/startTestTask")
    public WpResult startTestTask(String ex){
        SchedulerTask schedulerTask = new SchedulerTask();
        schedulerTask.setTaskId("t1");
        schedulerTask.setTaskName("测试任务");
        schedulerTask.setRunnable(new SchedulerRunnable(schedulerTask) {
            @Override
            public void runStart(){
                try {
                    Thread.sleep(3000);
                    System.err.println("ex = " + ex);
                    if("1".equals(ex)){
                        System.err.println("error doing ");
                        String s = null;
                        s.toString();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.err.println(DateUtil.now() + " " + Thread.currentThread().getName() + ": test task running........");
            }

            // 只需赋值taskId,cron,taskName即可
            // 启动任务时会先执行一次
            @Override
            public void runFinish(SchedulerTask schedulerTask) {
                schedulerTask.setTaskName("测试任务2");
                schedulerTask.setCron("0/10 * * * * ?");
            }
        });
        schedulerService.startTask(schedulerTask,true);
        return new WpResult().success(schedulerTask);
    }

    /**
     * 启动任务
     * @return
     */
    @GetMapping("/startTask")
    public WpResult startTask(String taskId){
        SchedulerTask schedulerTask = schedulerService.startTask(taskId);
        return new WpResult().success(schedulerTask);
    }

    /**
     * 终止任务
     * @return
     */
    @GetMapping("/stopTask")
    public WpResult stopTask(String taskId){
        SchedulerTask schedulerTask = schedulerService.stopTask(taskId);
        return new WpResult().success(schedulerTask);
    }

    /**
     * 删除任务
     * @return
     */
    @GetMapping("/removeTask")
    public WpResult removeTask(String taskId){
        SchedulerTask schedulerTask = schedulerService.removeTask(taskId);
        return new WpResult().success(schedulerTask);
    }

    /**
     * 获取正在运行的任务
     * @return
     */
    @GetMapping("/getRunning")
    public WpResult getRunning(){
        List<SchedulerTask> schedulerTaskList = schedulerService.getRunnerTaskList();
        return new WpResult().success(schedulerTaskList);
    }

    /**
     * 查询任务运行状态
     * @return
     */
    @GetMapping("/isRunning")
    public WpResult isRunning(String taskId){
        boolean isRunning = schedulerService.isRunning(taskId);
        return new WpResult().success("任务["+ taskId +"]运行状态：" + isRunning);
    }

    /**
     * 获取所有任务
     * @return
     */
    @GetMapping("/getTaskList")
    public WpResult getTaskList(){
        List<SchedulerTask> schedulerTaskList = schedulerService.getTaskList();
        return new WpResult().success(schedulerTaskList);
    }

    /**
     * 根据ID查询任务
     * @return
     */
    @GetMapping("/getTask")
    public WpResult getTask(String taskId){
        SchedulerTask schedulerTask = schedulerService.getTaskById(taskId);
        return new WpResult().success(schedulerTask);
    }

    /**
     * 定时器运行报表
     * @return
     */
    @GetMapping("/report")
    public String report(String title,String time){
        String report = schedulerService.report(title,time);
        return report;
    }

    /**
     * 运行报表发送邮件
     * @return
     */
    @GetMapping("/sendReportEmail")
    public WpResult sendReportEmail(String title,String time){
        schedulerService.sendReportEmail(title,time);
        return new WpResult().success("发送成功");
    }

}
